var resRef={
    player:'player',
    vertical_wall:'wallV',
    horizontal_wall:'wallH',
    coin:'coin',
    enemy:'enemy',
    background:'background'
};
var MainState={
    preload:function(){
        game.load.image(resRef.player,'res/player.png');
        game.load.image(resRef.vertical_wall,'res/wallVertical.png');
        game.load.image(resRef.horizontal_wall,'res/wallHorizontal.png');
        game.load.image(resRef.coin,'res/coin.png');
        game.load.image(resRef.enemy,'res/enemy.png');
    },
    create:function(){
        //game.scale.scaleMode=Phaser.ScaleManager.SHOW_ALL;
        game.stage.backgroundColor='#3498db';
        //Arcade physics is default
        //game.physics.startSystem(Phaser.Physics.ARCADE);
        this.player=game.add.sprite(game.world.centerX,game.world.centerY,resRef.player);
        this.player.anchor.setTo(0.5);
    //    add gravity to player
        game.physics.arcade.enable(this.player);
        this.player.body.gravity.y=500;
        this.cursor=game.input.keyboard.createCursorKeys();
        this.createWorld();
    //    add coin
        this.coin=game.add.sprite(60,140,'coin');
        game.physics.arcade.enable(this.coin);
        this.coin.anchor.setTo(0.5,0.5);
        //this.coin.body.gravity.y=100;
    //    add score
        this.scoreLabel=game.add.text(30,30,'score:0',{
            font:'18px Arial',fill:'#fff'
        });
        this.score=0;
    //    Create enemy
        this.enemies=game.add.group();
        this.enemies.enableBody=true;
        this.enemies.createMultiple(10,resRef.enemy);
        game.time.events.loop(2200,this.addEnemy,this);
    },
    addEnemy:function(){
        var enemy=this.enemies.getFirstDead();
        if(!enemy){
            return ;
        }
        enemy.anchor.setTo(0.5,1);
        enemy.reset(game.world.centerX,0);
        enemy.body.gravity.y=500;
        enemy.body.velocity.x=100*Phaser.Math.randomSign();
        enemy.body.bounce.x=1;
        enemy.checkWorldBounds=true;
        enemy.outOfBoundsKill=true;
    },
    update:function(){
        game.physics.arcade.collide(this.enemies,this.walls);
        game.physics.arcade.overlap(this.player,this.enemies,this.resetGame,null,this);
        game.physics.arcade.collide(this.player,this.walls);
        game.physics.arcade.overlap(this.player,this.coin,this.takeCoin,null,this);
        //game.physics.arcade.collide(this.walls,this.coin);
        this.movePlayer();
        if(!this.player.inWorld){
            this.resetGame();
        }
    },
    movePlayer:function(){
        if(this.cursor.left.isDown){
            this.player.body.velocity.x=-200;
        }else if(this.cursor.right.isDown){
            this.player.body.velocity.x=200;
        }else{
            this.player.body.velocity.x=0;
        }
        if(this.cursor.up.isDown&&this.player.body.touching.down){
            this.player.body.velocity.y=-350;
        }
    },
    createWorld:function(){
        this.walls=game.add.group();
        this.walls.enableBody=true;
        game.add.sprite(0,0,resRef.vertical_wall,0,this.walls);
        game.add.sprite(480,0,resRef.vertical_wall,0,this.walls);
        game.add.sprite(0,0,resRef.horizontal_wall,0,this.walls);
        game.add.sprite(300,0,resRef.horizontal_wall,0,this.walls);
        game.add.sprite(0,320,resRef.horizontal_wall,0,this.walls);
        game.add.sprite(300,320,resRef.horizontal_wall,0,this.walls);
        game.add.sprite(-100,160,resRef.horizontal_wall,0,this.walls);
        game.add.sprite(400,160,resRef.horizontal_wall,0,this.walls);

        var middleTop=game.add.sprite(100,80,resRef.horizontal_wall,0,this.walls);
        middleTop.scale.setTo(1.5,1);
        var middleBottom=game.add.sprite(100,240,resRef.horizontal_wall,0,this.walls);
        middleBottom.scale.setTo(1.5,1);
        this.walls.setAll('body.immovable',true);
    },
    resetGame:function(){
        game.state.start('main');
    },
    takeCoin:function(){
        this.updateCoinPosition();
        this.score+=5;
        this.scoreLabel.text='score: '+this.score;
    },
    updateCoinPosition:function(){
        var coinPosition=[
            {x:140,y:60},{x:360,y:60},
            {x:60,y:140},{x:440,y:140},
            {x:130,y:300},{x:370,y:300}
        ];
        for(var i=0;i<coinPosition.length;i++){
            if(coinPosition[i].x===this.coin.x){
                coinPosition.splice(i,1);
            }
        }
        var newPosition=coinPosition[
            game.rnd.integerInRange(0,coinPosition.length-1)
            ];
        this.coin.reset(newPosition.x,newPosition.y);
    }
};
var game=new Phaser.Game(500,340,Phaser.AUTO,'gameDiv');
game.state.add('main',MainState);
game.state.start('main');
